<template>
	<view class="root">
		<common-title :title="pageTitle" :showBack="false"></common-title>
		<scroll-view scroll-y="true" class="scroll-Y" v-if="!DataUtils.isEmpty(contactList)" refresher-enabled="true"
			:refresher-triggered="triggeredRef" :refresher-threshold="100" @refresherpulling="onPulling"
			@refresherrefresh="onRefresh" @refresherrestore="onRestore" @refresherabort="onAbort">
			<view class="view-contract">
				<view class="view-contract-item" v-for="(item, index) in contactList" :key="index"
					@click="clickContract(item)">
					{{item.name}}
				</view>

			</view>
		</scroll-view>



		<scroll-view scroll-y="true" class="scroll-Y" refresher-enabled="true" :refresher-triggered="triggeredRef"
			:refresher-threshold="100" @refresherpulling="onPulling" @refresherrefresh="onRefresh"
			@refresherrestore="onRestore" @refresherabort="onAbort" v-else>
			<NoData></NoData>
		</scroll-view>




	</view>

</template>

<script setup>
	import * as httpConfig from "../../common/http-config.js";
	import * as DataUtils from "../../utils/DataUtils";
	import CommonTitle from '@/components/common-title.vue';
	import NoData from '@/components/no-data.vue';
	import SelectBed from '@/components/select-bed.vue';
	import * as UploadUtils from '../../utils/UploadUtils.js';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		ref,
		onMounted,
		onUnmounted,
		computed,
		watch,
		defineComponent,
		getCurrentInstance,
		reactive
	} from 'vue'
	// 获取当前组件实例
	const instance = getCurrentInstance()
	// 初始化信息
	const initInfo = ref({})

	const triggeredRef = ref(true)

	const onPulling = (e) => {
		console.log("onPulling")

	}
	const onRefresh = () => {
		console.log("onRefresh")
		triggeredRef.value = true; // 开始刷新
		deviceContact()
	}

	const onRestore = () => {
		console.log("onRestore")
	}

	const onAbort = () => {
		console.log("onAbort")
	}



	onMounted(() => {
		console.log("组件已经挂载了.......")
		if (instance?.proxy?.$globalData?.initInfo) {
			let jsonStr = instance.proxy.$globalData.initInfo;
			console.log("onMounted jsonStr = " + jsonStr)
			initInfo.value = JSON.parse(jsonStr)
			initInfo.value["deviceCode"] = window.hb.getDeviceCode()
			console.log("onMounted initInfo = " + initInfo.value)
			deviceContact()
		}


	});
	onLoad((option) => {
		const deviceType = option.deviceType;
		const areaId = option.areaId;
		const deviceCode = option.deviceCode;


		console.log("deviceType = " + deviceType);
		console.log("areaId = " + areaId);
		console.log("deviceCode = " + deviceCode);
		if (!DataUtils.isEmpty(deviceType) && !DataUtils.isEmpty(areaId) && !DataUtils.isEmpty(deviceCode)) {
			// 通过 options 获取参数
			initInfo.value["deviceType"] = deviceType
			initInfo.value["areaId"] = areaId
			initInfo.value["deviceCode"] = deviceCode
			deviceContact()
		}

	})

	onUnmounted(() => {
		console.log("组件移除了.......")


	});
	const clickContract = ((item) => {
		console.log("clickContract " + item);
		window.hb.tryToCallContact(JSON.stringify(item))

	});
	const pageTitle = ref("通讯录")
	// {
	// 			"name": "PDA",
	// 			"sipNo": "6994",
	// 			"ip": "10.111.16.8",
	// 			"type": 8

	// 		}
	const contactList = ref([

	])

	//  分页获取消息提醒列表
	const deviceContact = () => {
		contactList.value = []
		let data = {
			"deviceType": initInfo.value.deviceType,
			"areaId": initInfo.value.areaId,
			"deviceCode": initInfo.value.deviceCode,
		}
		uni.request({
			url: httpConfig.baseURL + httpConfig.deviceContact, // 通讯录
			method: 'POST', // 请求方法为 POST
			data: data,
			success: (res) => {
				triggeredRef.value = false; // 开始刷新
				console.log('deviceContact success');
				if (res.data.code == httpConfig.successCode) {
					let list = res.data.data
					if (!DataUtils.isEmpty(list)) {
						list.forEach(item => {
							if (!DataUtils.isEmpty(item.contacts)) {
								item.contacts.forEach(i => {
									i.type = item.type
									contactList.value.push(i)
								})
							}


						});


					}

				} else {
					uni.showToast({
						title: it.data.message,
						icon: "success",
						duration: 2000
					});
				}
				// 在这里处理请求成功的情况
			},
			fail: (err) => {
				triggeredRef.value = false; // 开始刷新
				uni.hideLoading()
				console.error('deviceContact fail ');
				uni.showToast({
					title: "获取通讯录失败" + err,
					icon: "success",
					duration: 2000
				});
				// 在这里处理请求失败的情况
			}
		});
	}
</script>

<style lang="less" scoped>
	@vw: 8.015vw;
	@vh: 5.4vh;

	.root {
		height: 100vh;
		/* 页面高度为 100% */

		/* 禁止页面滚动 */
		display: flex;
		flex-direction: column;

		scroll-view {

			height: 100%;
			overflow: auto;

			.view-contract {
				width: 100%;
				display: flex;
				flex-wrap: wrap;
				/* 允许子元素换行 */
				justify-content: flex-start;


				.view-contract-item {
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: center;
					font-size: 16/@vh;
					border-radius: 4/@vh;
					margin-left: 20/@vh;
					margin-bottom: 20/@vh;

					width: 90/@vw;
					height: 48/@vh;
					background-color: white;
					border: 0.5/@vh solid #999;
					color: #2C2E30;
				}
			}


		}

	}
</style>